package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;

    //处理查询所有部门请求

    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageResult<Classinfo> pageResult = classinfoService.query(qo);
        model.addAttribute("pageResult", pageResult);
        // 找视图 /WEB-INF/views/classinfo/list.jsp
        return "classinfo/list";
    }


    //处理删除请求
    @RequestMapping("/delete")
    @ResponseBody

    public JsonResult delete(Long id) {
        try {

            classinfoService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }

    }

    //去新增或去修改页面

    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Classinfo classinfo = classinfoService.get(id);
            model.addAttribute("classinfo", classinfo);
        }
        return "classinfo/input";
        //让浏览器重新发一次请求
    }

    //新增或修改

    @RequestMapping("/saveOrUpdate")

    public String saveOrUpdate(Classinfo classinfo) {

            if (classinfo.getId() == null) {//新增
                classinfoService.save(classinfo);
            } else {
                classinfoService.update(classinfo);//修改
            }


            return "redirect:/classinfo/list.do";
            //让浏览器重新发一次请求

        }
    }

